package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.query.StrategyDetailQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private IDestinatonService destinatonService;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    public PageInfo query(StrategyDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void deleteById(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdate(StrategyDetail strategyDetail,String tags) {
        //设置是否为海外,就要获取destId才行,根据strategyId获取destId
        Long strategyId = strategyDetail.getStrategy().getId();
        Long destId = strategyMapper.getDestIdByStrategyId(strategyId);
        boolean ret =  destinatonService.isAbroad(destId);
        strategyDetail.setIsabroad(ret);
        //添加目的地Id
        Destination dest = new Destination();
        dest.setId(destId);
        strategyDetail.setDest(dest);
        //获取内容,并添加摘要
        String content = strategyDetail.getStrategyContent().getContent();
        if(content.length()>200){
            strategyDetail.setSummary(content.substring(0,200));
        }else{
            strategyDetail.setSummary(content);
        }
        if(strategyDetail.getId() == null){
            //设置创建时间
            strategyDetail.setCreateTime(new Date());
            //新增操作
            strategyDetailMapper.insert(strategyDetail);
            //根据strategyDetail的id去添加内容表strategy_content
            Long id = strategyDetail.getId();
            StrategyContent strategyContent = new StrategyContent();
            strategyContent.setId(id);
            strategyContent.setContent(content);
            strategyContentMapper.insert(strategyContent);
        }else{
            //更新操作
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //根据strategyDetail的id去更新内容表strategy_content
            Long id = strategyDetail.getId();
            StrategyContent strategyContent = new StrategyContent();
            strategyContent.setId(id);
            strategyContent.setContent(content);
            strategyContentMapper.update(strategyContent);
            //更新操作的时候,先删除第三张表中关系
            strategyTagMapper.deleteRelationDetailAndTagByDetailId(strategyDetail.getId());
        }


        //处理标签,将标签添加进去,并维护第三张表的关系
        String[] tagArr = tags.split(",");
        for (String tag : tagArr) {
            StrategyTag strategyTag = new StrategyTag();
            strategyTag.setName(tag);
            strategyTag.setState(StrategyTag.STATE_NORMAL);
            //添加进去
            strategyTagMapper.insert(strategyTag);
            //维护第三张表的关系
            strategyTagMapper.insertRalationDetailAndTag(strategyTag.getId(),strategyDetail.getId());
        }
    }

    public void changeState(int state, Long id) {
        strategyDetailMapper.changeState(state,id);
    }

    public StrategyDetail getById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public List<StrategyDetail> getDetailsByDestId(Long destId) {
        return strategyDetailMapper.selectDetailsByDestId(destId);
    }

    public StrategyContent getContentById(Long id) {
        return strategyContentMapper.selectContentById(id);
    }

    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.selectAll();
    }

    public void updateStatisData(PersistenceStrategyStatisVO psvo) {
        strategyDetailMapper.updateStatisData(psvo);
    }
}
